package com.yyk.yykywd.ui.work;

import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.base.BaseRecyclerAdapter;
import com.yyk.yykywd.base.BaseRecyclerHolder;
import com.yyk.yykywd.bean.WorkPlandetailInfo;
import com.yyk.yykywd.config.Constant;
import com.yyk.yykywd.config.HttpUrl;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.listenter.ToolbarListenter;
import com.yyk.yykywd.ui.WebActivity;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.OnClickFlagUtil;
import com.yyk.yykywd.widght.diver.more.SpacesItemDecoration;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 查看待签合同
 */
public class QuerySignedctActivity extends BaseActivity {
    public static QuerySignedctActivity intance;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_createtime)
    TextView tvCreatetime;
    @BindView(R.id.tv_topname)
    TextView tvTopname;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_time_end)
    TextView tvTimeEnd;
    @BindView(R.id.tv_goal)
    TextView tvGoal;
    @BindView(R.id.tv_rests)
    TextView tvRests;
    @BindView(R.id.rv_udate)
    RecyclerView rvUdate;
    @BindView(R.id.tv_createtime2)
    TextView tvCreatetime2;
    @BindView(R.id.tv_name2)
    TextView tvName2;
    @BindView(R.id.tv_phone2)
    TextView tvPhone2;
    @BindView(R.id.tv_postname2)
    TextView tvPostname2;
    @BindView(R.id.tv_describe2)
    TextView tvDescribe2;
    @BindView(R.id.tv_remark2)
    TextView tvRemark2;
    @BindView(R.id.rv_udate3)
    RecyclerView rvUdate3;
    @BindView(R.id.tv_remark3)
    TextView tvRemark3;
    @BindView(R.id.tv_createtime3)
    TextView tvCreatetime3;
    @BindView(R.id.ll_ywgzlc)
    LinearLayout llYwgzlc;
    private String id;
    private String intentflag = "0";

    @Override
    public int getLayoutId() {
        return R.layout.activity_query_signedct;
    }

    @Override
    public void initView() {
        initToolbar();

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        //暂时判断是否客户拜访跳转过来
        if (intentflag.equals("0")) {
            finish();
        } else {
            startAct(SignedContractActivity.class, true);
        }
    }

    private void initToolbar() {
        setBackArrow(new ToolbarListenter() {
            @Override
            public void onClickListenter() {
                if (intentflag.equals("0")) {
                    finish();
                } else {
                    startAct(SignedContractActivity.class, true);
                }
            }
        });
        setTitle("待签合同");
    }

    @Override
    public void initData() {
        intance = this;
        initGetIntent();
        postWorkPlandetail(true);

    }

    private void initGetIntent() {
        if (getBundle() != null) {
            id = getBundle().getString("id");
            boolean intentFlag = getBundle().containsKey("intentflag");
            if (intentFlag) {
                intentflag = getBundle().getString("intentflag");
            }
        }
    }

    /**
     * 接口4：查看业务详情（whm）
     *
     * @param flag
     */
    private void postWorkPlandetail(final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("uid", Hawk.get("uid")+"");
        map.put("bid", id + "");
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postWorkPlandetail(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<WorkPlandetailInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(WorkPlandetailInfo info) {
                        Logger.e("查看业务详情 待签合同" + info.toString());
                        if (info.getCode() == 1) {
                            //1.业务计划
                            WorkPlandetailInfo.DataBean.PlanBean plan = info.getData().getPlan();

                            //业务编号
                            String number = plan.getNumber();
                            //创建时间
                            String createtime = plan.getCreatetime();
                            //业务类型
                            String topname = plan.getTopname();
                            //机构名称
                            String name = plan.getName();
                            tvCreatetime.setText(createtime);
                            tvGoal.setText(plan.getGoal());
                            tvNumber.setText(number);
                            tvRests.setText(plan.getRests());
                            if (plan.getEnd() == 1) {
                                tvTimeEnd.setText(plan.getTime() + "   上午");
                            } else if (plan.getEnd() == 2) {
                                tvTimeEnd.setText(plan.getTime() + "   下午");
                            } else {
                                tvTimeEnd.setText(plan.getTime() + "   晚上");
                            }
                            tvTopname.setText(topname);
                            tvName.setText(name);
                            List<WorkPlandetailInfo.DataBean.PlanBean.UdataBean> udata = plan.getUdata();
                            BaseRecyclerAdapter<WorkPlandetailInfo.DataBean.PlanBean.UdataBean> adapter = new BaseRecyclerAdapter<WorkPlandetailInfo.DataBean.PlanBean.UdataBean>(mActivity, udata, R.layout.adapter_item_businessplan_deteail) {
                                @Override
                                public void convert(BaseRecyclerHolder holder, WorkPlandetailInfo.DataBean.PlanBean.UdataBean item, int position, boolean isScrolling) {
                                    holder.setImageByUrl(R.id.iv_img, item.getImg());
                                    holder.setText(R.id.tv_uname, item.getUname());
                                }
                            };
                            rvUdate.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false));
                            rvUdate.setAdapter(adapter);
                            //2.拜访信息
                            WorkPlandetailInfo.DataBean.VisitBean visit = info.getData().getVisit();
                            tvCreatetime2.setText(visit.getCreatetime());
                            tvName2.setText(visit.getName());
                            tvPhone2.setText(visit.getPhone());
                            tvDescribe2.setText(visit.getDescribe());
                            tvPostname2.setText(visit.getPost_name());
                            tvRemark2.setText(visit.getRemark());
                            //3.项目申请书信息
                            WorkPlandetailInfo.DataBean.ApplyBean apply = info.getData().getApply();
                            tvCreatetime3.setText(apply.getCreatetime());
                            List<String> img3 = apply.getImg();//图片
                            String remark3 = apply.getRemark();//备注
                            tvRemark3.setText(remark3);
                            BaseRecyclerAdapter<String> adapter3 = new BaseRecyclerAdapter<String>(mActivity, img3, R.layout.adapter_item_shenqingimg_deteailb) {
                                @Override
                                public void convert(BaseRecyclerHolder holder, String item, int position, boolean isScrolling) {
                                    holder.setImageByUrl(R.id.iv_img, item);
                                }
                            };
                            rvUdate3.setLayoutManager(new GridLayoutManager(mActivity, 4, GridLayoutManager.VERTICAL, false));
                            rvUdate3.addItemDecoration(new SpacesItemDecoration(14, 0));
                            rvUdate3.setAdapter(adapter3);


                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @OnClick({R.id.bt_yewugenjin, R.id.bt_upload, R.id.ll_ywgzlc})
    public void onViewClicked(View view) {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.bt_yewugenjin:
                Bundle bundle = new Bundle();
                bundle.putString("id", id + "");
                startAct(BusinessToGoActivity.class, bundle);
                break;
            case R.id.bt_upload:
                Bundle bundle2 = new Bundle();
                bundle2.putString("id", id + "");
                startAct(UploadHtmessageActivity.class, bundle2);
                break;
            case R.id.ll_ywgzlc:
                Intent intent = new Intent();
                //业务跟踪流程
                intent.putExtra("url", HttpUrl.BASE_H5_URL + "business/?uid=" + Hawk.get("uid") + "&did=" + id);
                startAct(intent, WebActivity.class, null);
                break;
        }
    }


}
